package activity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

//import com.google.gson.Gson;
import com.example.administrator.ci_edit.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.FCAdapter;
import model.Community;
import model.Constants;
import okhttp3.Call;

/**
 * Created by mary on 2017/7/14.
 */

public class ShowFCActivity extends ListActivity {
    private ListView listView;
    //Gson gson;
    private String[] data = {"apple", "banana", "Orange", "Mango", "Watermelon", "pear", "grape", "pinapple", "a", "b", "c", "d"};
    String content;
    private List<Community> friendsCircleList=new ArrayList<Community>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fc_template);
        listView = (ListView) findViewById(android.R.id.list);
        OkHttpUtils.get()
                .url(Constants.SERVER_URL+"load")
                .addParams("flag","-1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(final String response, int id) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    initFC(response);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }).start();
                    }
                });
        FCAdapter adapter=new FCAdapter(ShowFCActivity.this,R.layout.item,friendsCircleList);
        listView.setAdapter(adapter);
        Toast.makeText(ShowFCActivity.this, "sda", Toast.LENGTH_SHORT).show();

    }
    public void initFC(String json) throws JSONException {

        JSONObject jsonObject = new JSONObject(json);
        JSONObject data=jsonObject.getJSONObject("poems");
        for(int i=0;i<data.length();i++){
            JSONObject info = data.getJSONObject(Integer.toString(i));
            Community community =new Community(info.getString("poem"),info.getString("time"),info.getString("username"),info.getString("tune"));
            friendsCircleList.add(community);
        }


    }


}
